import { Node, Sprite, UITransform, view, type Asset } from 'cc'
import { Logger } from './logger'

const logger = new Logger('适配器')
export const fitContainer = (node: Node) => {
  const resource = node.getComponent(Sprite)
  const UI = node.getComponent(UITransform)
  const { width, height } = view.getVisibleSize()
  if (resource) {
    const { width: imgWidth, height: imgHeight } = resource.spriteFrame
    const ratio = imgWidth / imgHeight
    const bool = width / height > ratio
    logger.debug('宽高比 %o', bool, ratio)
    if (bool) {
      UI.setContentSize(width, width / ratio)
    } else {
      UI.setContentSize(height * ratio, height)
    }
  }
}
